<!DOCTYPE html>
<html>
<head>
	<title>注册</title>
	<link rel="stylesheet" type="text/css" href="../css/register.css">
</head>
<body>
	<div id="logo">
		<img src="../img/tamp.png">
	</div>
	<div id="center">
		<div id="head">
			<strong>注册新用户</strong><br>
			<span>注册后您可以进行用户登录!</span>
		</div>
		<form method="post">
			<input type="text" class="i1" name="username" placeholder="用户名*" id="username"><br>
			<input type="password" class="i1" name="password" placeholder="密码*" id="password"><br>
			<input type="password" class="i2" name="" placeholder="确认密码*" id="repassword"><br>
			<input type="submit" class="s1" name="" value="立即注册" onclick="return submit_form()">
			<a href="login.php" class="a1">已有帐号？直接登录</a>
		</form>
	</div>
    <script type="text/javascript">
        function submit_form() {
            let name = document.getElementById("username").value;
            let pwd = document.getElementById("password").value;
            let rpwd = document.getElementById("repassword").value;
            if(name == '' || pwd == '' || rpwd == ''){
                alert("请输入完整！");
                return false;
            }else if(pwd != rpwd){
                alert("密码不一致！");
                return false;
            }
        }
    </script>
</body>
</html>
<?php
//header('Refresh: 3; url=login.php');  xs后跳转。

include_once 'mysql.php';//执行时连接
/*include 'sql.php';//一直连接*/
    if(!empty($_POST)){
        $mysql = new mysql();
        $name = $_POST['username'];
        $pwd = $_POST['password'];
        $select_sql = 'select username from test_table where username ="'.$name.'"';
        $sql_q = $mysql->select_one($select_sql);
        if(empty($sql_q)){
            //'INSERT INTO test_table (`username`, `password`) VALUES ("'.$name.'","'.$pwd.'");';
            $data = array(
                'username' => $name,
                'password' => $pwd
            );
            $table = 'test_table';
            $mysql->insert($data, $table);
            echo '<script type="text/javascript">var mymessage=confirm("注册成功！立即登录？");
                if(mymessage==true){
                     window.location.href="login.php";
                }
                </script>';
        }else{
            echo '<script type="text/javascript">alert("此用户名已存在！");</script>';
        }
    }
?>